<?php
	// Author: Andy Marti
	// Datum: 12.03.2014
	// ----------------------------------------------------
	// Beschreibung Klasse:
	//   Die Klasse enthält alle Eigenschaften und Methoden für den Zugriff auf die DB. 
	//   Sie wird in der Klasse User erzeugt.
	// ----------------------------------------------------
	// Änderungen:
	//
	// ----------------------------------------------------

	require_once 'config.php';

	/**
	 * @author Andy Marti
	 *
	 */
	class UserDb extends PDO{
		// ------------------------------------------------
		/**
		 * Die Eigenschaften der Klasse.
		 *
		 * @var Messages
		 */
		// Public Variables.
		// Public Objects.
		public $Messages = NIL;

		// ------------------------------------------------
		/**
		 * Die Eigenschaften der Klasse.
		 * 
		 * @var 
		 *
		 */
		// Public Objects.
		// Private Objects.
		private $Conn = NIL;

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle User-Felder aus der DB.
		 * 
		 * @param User $User
		 * @param string $extuserid
		 */
		public function getUser($user, $extuserid, $loggedinuserid) {
			if ($user != NIL) {
				$hasAccess = $this->hasAccessRules($loggedinuserid);
				$ownUser = $extuserid == $loggedinuserid;
				$sql = "SELECT * FROM User_AllFields WHERE ExternalUser_ID ='$extuserid';";
				$query = $this->Conn->prepare($sql);
				$query->execute();
				if ($query->rowCount() > 0) {
					if ($query->rowCount() == 1) {
						// Get row from result.
						$row = $query->fetchAll(PDO::FETCH_ASSOC);
						// Get fields of User.
						$user->UserName = $row[0]['UserName'];
						$user->Title = $row[0]['Title'];
						$user->FirstName = $row[0]['FirstName'];
						$user->LastName = $row[0]['LastName'];
						If ($ownUser or $hasAccess or ($row[0]['VisBirthday'])) $user->Birthday = $this->formatDate($row[0]['Birthday'], 'd.m.Y'); 
						$user->Age = $row[0]['Age'];
						$user->Description = $row[0]['Description'];
						$user->ImagePath = $row[0]['ImagePath'];
						$user->ImageWidth = $row[0]['ImageWidth'];
						$user->ImageHeigth = $row[0]['ImageHeigth'];
						$user->State = $row[0]['State'];
						$user->UserID = $row[0]['User_ID'];
						$user->AddressID = $row[0]['Address_ID_FK'];
						$user->ImageID = $row[0]['Image_ID_FK'];
						$user->ContactID = $row[0]['Contact_ID_FK'];
						// Get fields of AccessRule.
						$user->AccessRuleID = $row[0]['AccessRuleID'];
						$user->AccessRule = $row[0]['AccessRule'];
						// Get fields of Address.
						$user->Address->Address = $row[0]['Address'];
						$user->Address->PLZ = $row[0]['PLZ'];
						$user->Address->City = $row[0]['City'];
						$user->Address->Country = $row[0]['Country'];
						// Get fields of Contact.
						$user->Contact->Contact_ID = $row[0]['Contact_ID'];
						$user->Contact->TelefonPrivate = $row[0]['TelefonPrivate'];
						If ($ownUser or $hasAccess or ($row[0]['VisTelefonBusiness'])) $user->Contact->TelefonBusiness = $row[0]['TelefonBusiness'];
						If ($ownUser or $hasAccess or ($row[0]['VisTelefonMobile'])) $user->Contact->TelefonMobile = $row[0]['TelefonMobile'];
						If ($ownUser or $hasAccess or ($row[0]['VisEmailPrivate'])) $user->Contact->EmailPrivate = $row[0]['EmailPrivate'];
						If ($ownUser or $hasAccess or ($row[0]['VisEmailBusiness'])) $user->Contact->EmailBusiness = $row[0]['EmailBusiness'];
						If ($ownUser or $hasAccess or ($row[0]['VisWebsitePrivate'])) $user->Contact->WebsitePrivate = $row[0]['WebsitePrivate'];
						If ($ownUser or $hasAccess or ($row[0]['VisWebsiteBusiness'])) $user->Contact->WebsiteBusiness = $row[0]['WebsiteBusiness'];
					} else {
						$this->Messages->throwMessage($this->Messages->NumDbUserIdNotUnique, $extuserid);
					} 
				} else {
					$this->Messages->throwMessage($this->Messages->NumDbUserNotFound, $extuserid);
				} 
			} else {
				$this->Messages->throwMessage($this->Messages->$MsgNotInitialized, $user);
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Member-Felder aus der DB.
		 *
		 * @param Member $member
		 * @param Integer $userid
		 */
		public function getMemberData($member, $userid) {
			if ($member != NIL) {
				$sql = "SELECT * FROM member_allfields WHERE UserID=".$userid;
				$query = $this->Conn->prepare($sql);
				$query->execute();
				if ($query->rowCount() > 0) {
					// Get row from result.
					$row = $query->fetchAll(PDO::FETCH_ASSOC);
					// Get fields of Memberdata.
					$member->MemberFee = $row[0]['MemberFee'];
					$member->MemberState = $row[0]['MemberState'];
					$member->MemberStateID = $row[0]['MemberState_ID'];
					$member->MemberID = $row[0]['MemberID'];
					$member->FamilyFlag = $row[0]['FamilyFlag'];					
				}
			} else {
				$this->Messages->throwMessage($this->Messages->$MsgNotInitialized, $member);
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Member-States aus der DB.
		 *
		 * 
		 */
		public function getMemberStates() {
			$sql = "SELECT * FROM MemberState";
			$query = $this->Conn->prepare($sql);
			$query->execute();
			if ($query->rowCount() > 0) {
				// Get rows from result.
				$result = array();
				$rows = $query->fetchAll(PDO::FETCH_ASSOC);
				foreach ($rows as $row) {
					$result[$row['MemberState_ID']] = $row['StateName'];
				}
				return $result;
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Lizenzen aus der DB.
		 *
		 *
		 */
		public function getAssociationTypes() {
			$sql = "SELECT * FROM AssociationType";
			$query = $this->Conn->prepare($sql);
			$query->execute();
			if ($query->rowCount() > 0) {
				// Get rows from result.
				$result = array();
				$rows = $query->fetchAll(PDO::FETCH_ASSOC);
				foreach ($rows as $row) {
					$result[$row['AssociationType_ID']] = $row['AssociationTypeName'];
				}
				return $result;
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Job-Felder aus der DB.
		 *
		 * 
		 */
		public function getJobTypes() {
			$sql =	"SELECT * FROM JobType";
			$query = $this->Conn->prepare($sql);
			$query->execute();
			if ($query->rowCount() > 0) {
				// Get row from result.
				$rows = $query->fetchAll(PDO::FETCH_ASSOC);
				// Get fields of Jobdata.
				$result = array();
				foreach ($rows as $row) {
					$result[$row['Job_ID']] = $row['JobName'];
				}
				return $result;
			}
		}
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Vorstandsmitgliedertypen aus der DB.
		 *
		 *
		 */
		public function getCommitteeTaskTypes() {
			$sql = "SELECT * FROM CommitteeTaskType";
			$query = $this->Conn->prepare($sql);
			$query->execute();
			if ($query->rowCount() > 0) {
				// Get rows from result.
				$result = array();
				$rows = $query->fetchAll(PDO::FETCH_ASSOC);
				foreach ($rows as $row) {
					$result[$row['CommitteeTaskType_ID']] = $row['CommitteeTaskName'];
				}
				return $result;
			}
		}
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Settings-Felder aus der DB.
		 *
		 * @param Settings $settings
		 */
		public function getSettingsData($settings) {
			if ($settings != NIL) {
				$sql = "SELECT * FROM Settings;";
				$query = $this->Conn->prepare($sql);
				$query->execute();
				if ($query->rowCount() > 0) {
					// Get row from result.
					$rows = $query->fetchAll(PDO::FETCH_ASSOC);
					// Get fields of Settings.
					foreach ($rows as $row) {
						switch ($row['SettingsName']) {
							case 'InitialMemberAmount'		: $settings->InitialMemberAmount = $row['SettingsValue']; break;
							case 'MaxShareAssociationFee'	: $settings->MaxShareAssociationFee = $row['SettingsValue']; break;
							case 'FamilyDiscount'			: $settings->FamilyDiscount = $row['SettingsValue']; break;
							case 'CommitteeAmount'			: $settings->CommitteeAmount = $row['SettingsValue']; break;
							case 'OnceFAASAmountAdult'		: $settings->OnceFAASAmountAdult = $row['SettingsValue']; break;
							case 'OnceFAASAmountChild'		: $settings->OnceFAASAmountChild = $row['SettingsValue']; break;
							case 'Debug'					: $settings->Debug = $row['SettingsValue']; break;
						}
					}
				}
			} else {
				$this->Messages->throwMessage($this->Messages->$MsgNotInitialized, $settings);
			}
		}
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Association-Felder aus der DB.
		 *
		 * @param Association $association
		 * @param Integer $memberid
		 */
		public function getAssociations($association, $memberid) {
			if ($association != NIL) {
				$sql = "SELECT * FROM association_data WHERE MemberData_ID=".$memberid;
				$query = $this->Conn->prepare($sql);
				$query->execute();
				if ($query->rowCount() > 0) {
					// Get row from result.
					$rows = $query->fetchAll(PDO::FETCH_ASSOC);
					// Get fields of Associationdata.
					$association->AssociationList = array();
					foreach ($rows as $row) {
						$association->AssociationList[$row['AssociationTypeName']] = $row['AssociationNumber'];
					}
				}
			} else {
				$this->Messages->throwMessage($this->Messages->$MsgNotInitialized, $association);
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Job-Felder aus der DB.
		 *
		 * @param Job $job
		 * @param Integer $userid
		 */
		public function getJobs($job, $userid) {
			if ($job != NIL) {
				$sql =	"SELECT * FROM jobs_from_users ".
						"WHERE User_ID=".$userid;
				$query = $this->Conn->prepare($sql);
				$query->execute();
				if ($query->rowCount() > 0) {
					// Get row from result.
					$rows = $query->fetchAll(PDO::FETCH_ASSOC);
					// Get fields of Jobdata.
					$job->JobList = array();
					foreach ($rows as $row) {
						$res['UserID'] = $row['User_ID'];
						$res['JobID'] = $row['Job_ID'];
						$res['JobType'] = $row['JobName'];
						$job->JobList[] = $res;
					}
				}
			} else {
				$this->Messages->throwMessage($this->Messages->$MsgNotInitialized, $job);
			}
		}
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle CommitteeTask-Felder aus der DB.
		 *
		 * @param CommitteeTask $committeetask
		 * @param Integer $memberid
		 */
		public function getCommitteeTasks($committeetask, $memberid) {
			if ($committeetask != NIL) {
				$sql =	"SELECT * FROM CommitteeTask ".
						"LEFT JOIN CommitteeTaskType ON CommitteeTaskType_ID_FK=CommitteeTaskType_ID ".
						"WHERE MemberData_ID_FK=".$memberid;
				$query = $this->Conn->prepare($sql);
				$query->execute();
				if ($query->rowCount() > 0) {
					// Get row from result.
					$rows = $query->fetchAll(PDO::FETCH_ASSOC);
					// Get fields of CommitteeTaskdata.
					$committeetask->CommitteeTaskList = array();
					foreach ($rows as $row) {
						$res['MemberID'] = $row['MemberData_ID_FK'];
						$res['CommitteeTaskID'] = $row['CommitteeTaskType_ID_FK'];
						$res['CommitteeTaskType'] = $row['CommitteeTaskName'];
						$committeetask->CommitteeTaskList[] = $res;
					}
				}
			} else {
				$this->Messages->throwMessage($this->Messages->$MsgNotInitialized, $committeetask);
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Parsed alle Member-States aus der DB.
		 *
		 *
		 */
		public function hasAccessRules($extuserid) {
			$sql =	"SELECT COUNT(*) AS Count FROM AccessRules acr ".
					"LEFT JOIN User usr ON usr.User_ID = acr.User_ID_AR_FK ".
					"WHERE usr.ExternalUser_ID = '".$extuserid."'";
			$query = $this->Conn->prepare($sql);
			$query->execute();
			if ($query->rowCount() > 0) {
				$rows = $query->fetchAll(PDO::FETCH_ASSOC);
				return ($rows[0]['Count'] > 0);
			} else {
				return false;
			}
		}
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Speicher alle User-Felder in der DB.
		 *
		 * @param User $User
		 */
		public function setUser($user) {
			if ($user != NIL) {
				$sql = "UPDATE User SET ".
						"UserName = '".$user->UserName. "', ".
						"Title='".$user->Title. "', ".
						"FirstName='".$user->FirstName. "', ".
						"LastName='".$user->LastName. "', ".
						"Birthday='".preg_replace('/([0-9]{2})\.([0-9]{2})\.([0-9]{4})/', '\3-\2-\1', $user->Birthday). "', ".
						"Description='".$user->Description. "' ".
						"WHERE User_ID='$user->UserID';";
				$query = $this->Conn->prepare($sql);
				if ($query->execute() == 1) {
					// User updated. Now update the Locaton fields.
					$sql =	"UPDATE Location SET ".
							"Address='".$user->Address->Address."', ".
							"PLZ='".$user->Address->PLZ."', ".
							"City='".$user->Address->City."', ".
							"Country='".$user->Address->Country."' ".
							"WHERE Location_ID='".$user->AddressID."';";
					$query = $this->Conn->prepare($sql);
					if ($query->execute() == 1) {
						// Address updated. Now update Contact fields.
						$sql =	"UPDATE Contact SET ".
								"TelefonPrivate='".$user->Contact->TelefonPrivate."', ".
								"TelefonBusiness='".$user->Contact->TelefonBusiness."', ".
								"TelefonMobile='".$user->Contact->TelefonMobile."', ".
								"EmailPrivate='".$user->Contact->EmailPrivate."', ".
								"EmailBusiness='".$user->Contact->EmailBusiness."', ".
								"WebsitePrivate='".$user->Contact->WebsitePrivate."', ".
								"WebsiteBusiness='".$user->Contact->WebsiteBusiness."' ".
								"WHERE Contact_ID=".$user->ContactID.";";
						$query = $this->Conn->prepare($sql);
						if ($query->execute() < 1) {
							$this->Messages->throwMessage($this->Messages->NumDbUpdateQueryFailed, 'Contact');
						}
					} else {
						$this->Messages->throwMessage($this->Messages->NumDbUpdateQueryFailed, 'Address');
					}
				} else {
					$this->Messages->throwMessage($this->Messages->NumDbUpdateQueryFailed, 'User');
				}
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $user);
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Speicher alle User-Felder in der DB.
		 *
		 * @param User $User
		 */
		public function newUser($user) {
			if ($user != NIL) {
				// Start Transaction, delete all, insert all.
				$this->Conn->beginTransaction();
				
				// Insert address and get the ID.
				$sql = "SELECT setAddress('".$user->Address->Address."', '".
											 $user->Address->PLZ."', ".
											 $user->Address->City."', '".
											 $user->Address->Country."') ".
											"AS AddressID;";
				$query = $this->Conn->prepare($sql);
				$query->execute();
				if ($query->rowCount() > 0) {
					// Get rows from result and need the first (should be lonly).
					$rows = $query->fetchAll(PDO::FETCH_ASSOC);
					$user->AddressID = $rows[0]['AddressID'];
					
					// Insert contact data and get the ID.
					$sql =	"SELECT setContact('".$user->Contact->TelefonPrivate."', '".
												$user->Contact->TelefonBusiness."', '".
												$user->Contact->TelefonMobile."', '".
												$user->Contact->EmailPrivate."', '".
												$user->Contact->EmailBusiness."', '".
												$user->Contact->WebsitePrivate."', '".
												$user->Contact->WebsiteBusiness."') ".
												"AS ContactID;";
					$query = $this->Conn->prepare($sql);
					$query->execute();
					if ($query->rowCount() > 0) {
						// Get rows from result and need the first (should be lonly).
						$rows = $query->fetchAll(PDO::FETCH_ASSOC);
						$user->ContactID = $rows[0]['ContactID'];
						
						// Insert User.
						$sql =	"SELECT setUser('".$user->UserName."', '".
													$user->Title."', '".
													$user->FirstName."', '".
													$user->LastName."', ".
													preg_replace('/([0-9]{2})\.([0-9]{2})\.([0-9]{4})/', '\3-\2-\1', $user->Birthday).", '".
													$user->Description."', ".
													$user->AddressID.", ".
													$user->ContactID.", ".
													$user->ImageID.", '".
													$user->ExternalUserID."', ".
													$user->State.") ".
													"AS UserID;";
						$query = $this->Conn->prepare($sql);
						$query->execute();
						if ($query->rowCount() > 0) {
							// Get rows from result and need the first (should be lonly).
							$rows = $query->fetchAll(PDO::FETCH_ASSOC);
							$user->UserID = $rows[0]['UserID'];
							
							// Everthing OK. Do commit.
							$this->Conn->commit();
							
						} else {
							$this->Conn->rollBack();
							$this->Messages->throwMessage($this->Messages->$NumDbInsertQueryFailed, 'User');
						}
					} else {
						$this->Conn->rollBack();
						$this->Messages->throwMessage($this->Messages->$NumDbInsertQueryFailed, 'Contact');
					}
				} else {
					$this->Conn->rollBack();
					$this->Messages->throwMessage($this->Messages->$NumDbInsertQueryFailed, 'Address');
				}
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $user);
			}
		}
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Speicher alle User-Felder in der DB.
		 *
		 * @param User $User
		 */
		public function setUserState($user) {
			if ($user != NIL) {
				$sql = "UPDATE User SET State=".$user->State." WHERE User_ID='".$user->UserID."';";
				$query = $this->Conn->prepare($sql);
				if ($query->execute() < 1) {
					$this->Messages->throwMessage($this->Messages->NumDbUpdateQueryFailed, 'UserState');
				}
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $user);
			}
		}
				
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Speicher alle Member-Felder in der DB.
		 *
		 * @param Member $Member
		 * 
		 */
		public function setMemberData($member) {
			if ($member != NIL) {
				// Update MemberData.
				$sql =	"UPDATE MemberData SET ".
						"MemberState_ID = ".$member->MemberStateID.", ".
						"FamilyFlag"." = ".$member->FamilyFlag." ".
						"WHERE MemberData_ID = ".$member->MemberID;
				$query = $this->Conn->prepare($sql);
				
				return ($query->execute() == 1);
			
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $member);
			}
		}

		/**
		 * Speicher alle Association-Felder in der DB.
		 *
		 * @param Member $member
		 * 
		 */
		public function setAssociations($member) {
			if ($member != NIL) {
				// Update Associations.
				// Start Transaction, delete all, insert all.
				$this->Conn->beginTransaction();
				
				$sql = "DELETE FROM Association WHERE MemberData_ID_FK=".$member->MemberID.";";
				$res = $this->Conn->exec($sql);
				if ($res !== false) {
					foreach ($member->Associations->AssociationList as $key=>$value) {
						$sql = "INSERT INTO Association (AssociationType_ID_FK, MemberData_ID_FK, AssociationNumber) ".
								"VALUES((SELECT AssociationType_ID FROM AssociationType WHERE AssociationTypeName LIKE '".$key."'), ".
								$member->MemberID.", '".$value."');";
						
						$res = $this->Conn->exec($sql);
						if ($res === false) {
							break;
						}
					}
				}
				if ($res) { $this->Conn->commit(); } 
				else { $this->Conn->rollBack(); }
				
				return $res;
				
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $member);
			}
		}

		/**
		 * Speicher alle Job-Felder in der DB.
		 *
		 * @param Member $member
		 * 
		 */
		public function setJobs($member) {
			if ($member != NIL) {
				// Update Jobs.
				// Start Transaction, delete all, insert all.
				$this->Conn->beginTransaction();

				$sql = "DELETE FROM Job WHERE User_ID_FK = ".$member->UserID.";";
				$res = $this->Conn->exec($sql);
				if ($res !== false) {
					foreach ($member->Jobs->JobList as $job) {
						$sql = "INSERT INTO Job (User_ID_FK, Job_ID_FK) ".
						"VALUES(".$job['UserID'].", ".$job['JobID'].");";
						
						$res = $this->Conn->exec($sql);
						if ($res === false) {
							break;
						}
					}					
				}	
				if ($res === false) { $this->Conn->rollBack(); }
				else { $this->Conn->commit(); }
				
				return $res;
					
				
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $member);
			}
		}

		/**
		 * Speicher alle CommitteeTask-Felder in der DB.
		 *
		 * @param Member $member
		 *
		 */
		public function setCommitteeTasks($member) {
			if ($member != NIL) {
				// Update CommitteeTasks.
				// Start Transaction, delete all, insert all.
				$this->Conn->beginTransaction();
		
				$sql = "DELETE FROM CommitteeTask WHERE MemberData_ID_FK = ".$member->MemberID.";";
				$res = $this->Conn->exec($sql);
				if ($res !== false) {
					foreach ($member->CommitteeTasks->CommitteeTaskList as $committeetask) {
						$sql = "INSERT INTO CommitteeTask (MemberData_ID_FK, CommitteeTaskType_ID_FK) ".
								"VALUES(".$committeetask['MemberID'].", ".$committeetask['CommitteeTaskID'].");";

						$res = $this->Conn->exec($sql);
						if ($res === false) {
							break;
						}
					}
				}	
				if ($res === false) { $this->Conn->rollBack(); }
				else { $this->Conn->commit(); }
		
				return $res;
					
		
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $member);
			}
		}
		
		// ------------------------------------------------
		// Public methodes.
		/**
		 * Speicher alle Settings-Felder in der DB.
		 *
		 * @param Settings $Settings
		 */
		public function setSettingsData($setting) {
			if ($setting != NIL) {
				$sql =	"UPDATE Settings SET SettingsValue=".$setting->InitialMemberAmount." WHERE SettingsName ='InitialMemberAmount';".
						"UPDATE Settings SET SettingsValue=".$setting->MaxShareAssociationFee." WHERE SettingsName ='MaxShareAssociationFee';".
						"UPDATE Settings SET SettingsValue=".$setting->FamilyDiscount." WHERE SettingsName ='FamilyDiscount';".
						"UPDATE Settings SET SettingsValue=".$setting->CommitteeAmount." WHERE SettingsName ='CommitteeAmount';".
						"UPDATE Settings SET SettingsValue=".$setting->OnceFAASAmountAdult." WHERE SettingsName ='OnceFAASAmountAdult';".
						"UPDATE Settings SET SettingsValue=".$setting->OnceFAASAmountChild." WHERE SettingsName ='OnceFAASAmountChild';".
						"UPDATE Settings SET SettingsValue=".$setting->Debug." WHERE SettingsName ='Debug';";
				$query = $this->Conn->prepare($sql);
				If ($query->execute() == 0) {
					$this->Messages->throwMessage($this->Messages->NumDbUpdateQueryFailed, 'Settings');
				}
			} else {
				$this->Messages->throwMessage($this->Messages->MsgNotInitialized, $setting);
			}
		}

		// ------------------------------------------------
		// Public methodes.
		/**
		 * Formatiert ein Datum mit entsprechendem Format.
		 *
		 *
		 */
		public function formatDate($date, $format) {
			If ($date != NIL) { 
	 			$birthday = new DateTime($date); 
	 			return $birthday->format($format);
		 	} else {
		 		return '';
		 	}
		}
		
		// ------------------------------------------------
		// Return Classname.
		public function __toString(){
			return get_class();
		}

		// ------------------------------------------------
		// Contructor, Destructor.
		/**
		 * Construtor.
		 *
		 */
		public function __construct($dsn, $username, $passwd, $options){
			try {
			 	$this->Conn = new PDO($dsn, $username, $passwd, $options);	
			} catch (PDOException $e) {
				print("Error on db connect! <br />");
				die();
			}
		}

		/**
		 * Destructor.
		 *
		 */
		function __destruct(){
			$Db = null;
		}
	}
?>